<template>
  <view class="page">
    <view class="logo">
      <image src="/static/logo.jpg" mode=""></image>
    </view>
    <!-- 填写区 -->
   <view class="" style="position: fixed;bottom: 40vh;width: 100%;left:0">
   	<!-- 按钮 -->
   	<view class="btn-info">
   	  <view class="btn" style="opacity:1" @click="tui.dianji(onLogin)">
   	    <text>授权登录</text>
   	  </view>
   	 
   	</view>
   	<!-- 操作 -->
   	
   	<!-- 其他方式登录 -->
   </view>
   
    
  </view>
</template>

<script>
export default {
  data() {
    return {
      isLogin: false,
      isLoginWay: false,
	  is_check:false,
      isPassword: false,
	  code:'',
	  inviter_code:'',
      // 表单
      form:{
        phone: '',
        code: '',
        password: '',
      },
    };
  },
  onLoad() {
	let that = this
  	let local = location.href;
  	that.code = that.getParam(local,'code')
  	that.inviter_code = that.getParam(local,'inviter_code')
  	if(that.inviter_code){
  		uni.setStorageSync("inviter_code", that.inviter_code)
  	}
  	if(that.code){
  		that.wetLogin()
  	}
  },
  methods:{
    onRegister(){
      uni.navigateTo({
        url: '/pages/register/register'
      })
    },
	getParam(path, name) {
		  var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");   
		  if (reg.test(path))  
		  return unescape(RegExp.$2.replace(/\+/g, " ")); 
		  return "";    
	},
	get_check(){
		this.is_check = !this.is_check
	},
	goUrl(e){
		this.tui.href(e)
	},
	wetLogin(){
		let that = this
		that.inviter_code=uni.getStorageSync("refCode")
		that.tui.request("api.wechat/wechatOpenidLogin", "POST", {code: that.code,inviter_code:that.inviter_code}, false, false, false).then((res) => {
			if (res.code == 1) {
				uni.setStorageSync('d_token', res.data.token)
				uni.setStorageSync('headimg', res.data.headimg)
				uni.setStorageSync('inviter_code', res.data.inviter_code)
				if(uni.getStorageSync('d_url')){
					let d_url = uni.getStorageSync('d_url')
					uni.removeStorage({
					    key: 'd_url',
					    success: function (res) {
					        console.log('success');
					    }
					});
					window.location.href =d_url
				}else if(uni.getStorageSync('playbackid')){
					that.tui.getList(uni.getStorageSync('playbackid'),4)
				}else if(uni.getStorageSync('zhiboid')){
					that.tui.getList(uni.getStorageSync('zhiboid'),2)
				}else if(uni.getStorageSync('shopId')){
					window.location.href =that.tui.faceUrl()+'#/pages/GoodsDetails/GoodsDetails?id='+uni.getStorageSync('shopId')
				}else{
					window.location.href =that.tui.faceUrl()+'#/pages/home/home'
				}
				setTimeout(function(){
					uni.removeStorage({
					    key: 'zhiboid',
					    success: function (res) {
					        console.log('success');
					    }
					});
					uni.removeStorage({
					    key: 'shopId',
					    success: function (res) {
					        console.log('success');
					    }
					});
				},1000)
			}
		}).catch((res) => {
		
		})
	},
    /**
     * 登录切换
     */
    onLoginCut(){
      this.isLoginWay = !this.isLoginWay;
      // 验证码
      if(this.isLoginWay){
        this.isLogin = this.form.code && this.form.phone ? true : false;
      }
      // 账号密码
      if(!this.isLoginWay){
        this.isLogin = this.form.password && this.form.phone ? true : false;
      }
    },
    /**
     * 登录点击
     */
    onLogin(){
		let that = this
		that.tui.wetLogin()
    }
  },
  watch:{
    form:{
      handler(newValue, oldValue) {
        // 验证码
        if(this.isLoginWay){
          this.isLogin = newValue.code && newValue.phone ? true : false;
        }
        // 账号密码
        if(!this.isLoginWay){
          this.isLogin = newValue.password && newValue.phone ? true : false;
        }
      },
      deep: true
    }
  }
}
</script>

<style scoped lang="scss">
@import 'login.scss';
</style>
